﻿using System.Text;

namespace Luka;

/// <summary>
/// base64编码
/// </summary>
public static class Base64Extensions
{
    /// <summary>
    /// Base64加密
    /// </summary>
    /// <param name="encode">加密采用的编码方式</param>
    /// <param name="source">待加密的明文</param>
    /// <returns>返回加密的文本</returns>
    public static string EncodeBase64(this string source, Encoding encode) => Convert.ToBase64String(encode.GetBytes(source));

    /// <summary>
    /// Base64加密，采用utf8编码方式加密
    /// </summary>
    /// <param name="source">待加密的明文</param>
    /// <returns>加密后的字符串</returns>
    public static string EncodeBase64(this string source) => source.EncodeBase64(Encoding.UTF8);

    /// <summary>
    /// Base64解密
    /// </summary>
    /// <param name="encode">解密采用的编码方式，注意和加密时采用的方式一致</param>
    /// <param name="result">待解密的密文</param>
    /// <returns>解密后的字符串</returns>
    public static string DecodeBase64(this string result, Encoding encode) => encode.GetString(Convert.FromBase64String(result));

    /// <summary>
    /// Base64解密，采用utf8编码方式解密
    /// </summary>
    /// <param name="result">待解密的密文</param>
    /// <returns>解密后的字符串</returns>
    public static string DecodeBase64(this string result) => result.DecodeBase64(Encoding.UTF8);
}